/* eslint-disable */
/******  创建axios实例 ******/
import axios from 'axios';
import qs from 'qs';
const service = axios.create({
    baseURL: 'http://wxjx.unisk.cn',
    // baseURL: '/api',
    timeout: 5000 //请求超时时间
})

/****** request拦截器==>对请求参数做处理 ******/
service.interceptors.request.use(config => {
    config.method == 'post' ? config.data = qs.parse(
        config.data
    ) : config.params = config.data;
    // console.log(config.data)
    // config.data = config.data;
    // config.method == 'post' ?
    //     config.headers['Content-Type'] = 'Content-Type:application/json;charset=utf-8' :
    //     config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
    // console.log(config)
    return config;
}, error => { //请求错误处理
    Promise.reject(error);
})

/****** respone拦截器==>对响应做处理 ******/
service.interceptors.response.use(
    response => { //成功请求到数据

        //这里根据后端提供的数据进行对应的处理
        if (response.data.code === '200') {
            // console.log(response.data)
            return response.data;
        } else if (response.data.code === '11002') {
            // console.log(response.data)
            location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf71dfa4539d2f215&redirect_uri=http%3A%2F%2Fwkfw.unisk.cn%2Fwxjx%2Fgame%2Fentrance%2FOv41jtk6fOabGlCy&response_type=code&scope=snsapi_base&state=&component_appid=wx9ca145ee3ab86a74#wechat_redirect'
        } else {
            layer.open({
                content: response.data.msg,
                skin: 'msg',
                time: 2 //2秒后自动关闭
            });
            return response.data;
        }
    },
    error => { //响应错误处理
        console.log('error');
        console.log(error);
        console.log(JSON.stringify(error));
        let text = JSON.parse(JSON.stringify(error)).response.status === 404 ?
            '404' :
            '网络异常，请重试';
        layer.open({
            content: text,
            skin: 'msg',
            time: 2 //2秒后自动关闭
        });
        return Promise.reject(error)
    }
);
export default service;